package devtool

import (
	"fmt"
	"reflect"
)

//json 字符串转为 model字符串
func JsonToModel(dat interface{}, modelName string) (string, []string) {
	fmt.Println("bbb", dat)
	if len(modelName) == 0 {
		modelName = "jsonModel"
	}
	modelName = camelString(modelName)
	modelstr := fmt.Sprintf("type %v struct {\n", modelName)
	outstr := ""
	outArr := make([]string, 0)
	fmt.Println("bbbw", reflect.TypeOf(dat))

	switch dat2 := dat.(type) {
	case map[string]interface{}:
		for k, v := range dat2 {
			fmt.Println("bbbcc", k, v)

			klengthstr := "\t"
			ck := camelString(k)
			if len(k) < 8 {
				klengthstr = "\t\t"
			}
			vtyp := reflect.TypeOf(v)
			// vk := reflect.ValueOf(k)
			// fmt.Printf("%v %v %v\n", pre, vtyp, v)
			// name := vtyp.Elem()
			fmt.Printf("%v %v %v name = %v\n", "", vtyp, v)
			// js := json.Number("")
			// km := reflect.TypeOf(js)
			if v == nil {
				modelstr = modelstr + fmt.Sprintf("\t%v%v%v\n", ck, klengthstr, "interface{}")
				continue
			}
			fmt.Println("bbbcc2", k, v)

			switch vtk := vtyp.Kind(); vtk {

			case reflect.Map:
				modelstr = modelstr + fmt.Sprintf("\t%v%v%vModel\n", ck, klengthstr, k)
				out1, out2 := JsonToModel(v, k+"Model")
				outstr = outstr + out1
				outArr = append(outArr, out2...)
			case reflect.Array, reflect.Slice:
				{
					res := v.([]interface{})
					modelstr = modelstr + fmt.Sprintf("\t%v%v[]%vModel\n", ck, klengthstr, k)
					if len(res) > 0 {
						out1, out2 := JsonToModel(res[0], k+"Model")
						outstr = outstr + out1
						outArr = append(outArr, out2...)

					}
				}
			default:
				{
					modelstr = modelstr + fmt.Sprintf("\t%v%v%v\n", ck, klengthstr, vtk)

				}

			}

		}

	}
	modelstr = modelstr + fmt.Sprintf("}\n")

	outArr = append([]string{modelstr}, outArr[0:]...)

	// fmt.Println(modelstr + outstr)
	return modelstr, outArr

}
